package observer;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布者实现类
 */
@Data
@AllArgsConstructor
public class PublisherImpl implements Publisher{
    private String account;
    private Integer score;
    private List<Observer> myObservers = new ArrayList<>();

    public PublisherImpl(String account, int score) {
        this.account =account;
        this.score = score;
    }

    @Override
    public void addObserver(Observer o) {
        myObservers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        myObservers.remove(o);
    }

    @Override
    public void notify(Integer score){
        this.score -= score;
        if (this.score <= 0){
            //发布通知
            not();
        }
    }

    private void not(){
        for (Observer observer : myObservers) {
            observer.notify(this.account,this.score);
        }
    }

}
